/**
 * class EntityListView: 
 * @param entityViews
 * @param pagination
 */
function EntityListView(controller, entityViews, pagination) {
	View.call(this, controller);
	
	this.addNewEntityView = function(entityView) {
		this.entityViews.push(entityView);
		this.addEntityView(entityView);
	};

	this.addEntityRow = function(entityView) {
		var tr = document.createElement("tr");
		var td = document.createElement("td");

		td.appendChild(entityView.tag);
		tr.appendChild(td);
		
		this.tbody.appendChild(tr);
	};
	
	/**
	 * public function addEntityView
	 * 
	 */
	this.addEntityView = function(entityView) {
		if (this.pagination != null) {
			this.pagination.addElement();
		} else {
			this.addEntityRow(entityView);
		}
	};

	/**
	 * public function setEntityViews
	 * @param entityViews
	 */
	this.setEntityViews = function(entityViews) {
		this.entityViews = entityViews;

		this.reset();

		for (var i=0 ; i<this.entityViews.length ; i++) {
			this.addEntityView(this.entityViews[i]);
		}
		
		if (this.getPagination() != null) {
			this.getPagination().showPage(0);
		}
	};

	this.reset = function() {
		// remove children
		while(this.tbody.hasChildNodes()) {
			this.tbody.removeChild(this.tbody.firstChild);
		}

		if (this.pagination != null) {
			this.pagination.reset();
		}
	};

	/**
	 * public function getPagination
	 * @return the pagination object
	 */
	this.getPagination = function() {
		return this.pagination;
	};
	
	/**
	 * public function setPagination
	 * @param pagination
	 * @throws Exception if the pagination object has less than 2 interfaces
	 */
	this.setPagination = function(pagination) {
		if (pagination.interfaces.length < 2) {
			// throw new Exception();
		}

		this.pagination = pagination;
		this.pagination.setHandler(this);

		// /////////////////////////////////////////////
		// Head
		var headtr = document.createElement("tr");
		var headtd = document.createElement("td");
		headtd.appendChild(pagination.getInterface(0).tag);
		headtr.appendChild(headtd);
		this.thead.appendChild(headtr);
		// /////////////////////////////////////////////
		
		// /////////////////////////////////////////////
		// Division
		var divisionTr = document.createElement("tr");
		var divisionTh = document.createElement("td");
		divisionTr.appendChild(divisionTh);
		var divisionHr = document.createElement("hr");
		divisionTh.appendChild(divisionHr);
		this.thead.appendChild(divisionTr);
		// /////////////////////////////////////////////

		// /////////////////////////////////////////////
		// Foot
		var foottr = document.createElement("tr");
		var foottd = document.createElement("td");
		foottd.appendChild(pagination.getInterface(1).tag);
		foottr.appendChild(foottd);
		this.tfoot.appendChild(foottr);
		// /////////////////////////////////////////////
	};

	this.showRange = function(range) {
		// remove children
		while(this.tbody.hasChildNodes()) {
			this.tbody.removeChild(this.tbody.firstChild);
		}
		
		for (var i=range.first ; i<range.last && i<this.entityViews.length ; i++) {
			var entityView = this.entityViews[i];
			this.addEntityRow(entityView);
		}
	};
	
	// /////////////////////////////////////////////
	// View part
	this.tag = document.createElement("table");
//	this.tag.border = 1;
	this.tag.width = "100%";

	this.thead = document.createElement("thead");
	this.tbody = document.createElement("tbody");
	this.tfoot = document.createElement("tfoot");
	
	this.tag.appendChild(this.thead);
	this.tag.appendChild(this.tbody);
	this.tag.appendChild(this.tfoot);
	// /////////////////////////////////////////////

	// /////////////////////////////////////////////
	// Data part (attributes)
	this.entityViews = null;
	this.pagination = null;
	// /////////////////////////////////////////////

	// /////////////////////////////////////////////
	// Constructor body
	if (pagination != undefined) {
		this.setPagination(pagination);
	}
	this.setEntityViews(entityViews);
	// /////////////////////////////////////////////
}
